/*
 * @Description:
 * @Author: chenju
 * @Date: 2021-11-08 15:37:26
 * @LastEditors: chenju
 * @LastEditTime: 2023-01-17 16:22:00
 */
import { get, post } from '@/utils/http';
import {
  AddOrganizationParams,
  AddUserOrganizationParams,
  GetOrganizationDetailResult,
  GetOrganizationListResult,
  GetOrganizationTreeResult,
  MoveOrganizationParams,
  EditOrganizationParams,
  GetUserListResult,
  GetChildFactoryOrgResult
} from './model/organizationModel';
enum Api {
  // 全部组织列表
  GET_ORGANIZATION_LIST = '/upms/organization/listOrganization',
  // 查询所有子组织列表
  GET_ORGANIZATION_CHILD_LIST = '/upms/organization/listChildByCode',
  // 添加组织
  ADD_ORGANIZATION = '/upms/organization/saveOrganization',
  // 编辑组织
  EDIT_ORGANIZATION = '/upms/organization/modifyOrganization',
  // 删除组织
  DELETE_ORGANIZATION = '/upms/organization/deleteOrganization',
  // 获取组织树列表
  GET_ORGANIZATION_TREE = '/upms/organization/getOrganizationTree',
  // 新增用户组织关联
  ADD_USER_ORGANIZATION = '/upms/organization/addUserOrganization',
  // 删除用户组织关联
  DELETE_USER_ORGANIZATION = '/upms/organization/deleteUserOrganization',
  // 查看组织详情
  GET_ORGANIZATION_DETAIL = '/upms/organization/getOrganization',
  // 移动组织
  MOVE_ORGANIZATION = '/upms/organization/moveOrganization',
  // 获取用户组织树
  GET_ORGANIZATION_TREE_BY_USER = '/upms/organization/getOrganizationTreeByUser',
  // 当前用户组织列表
  GET_CURRENT_ORGANIZATION_LIST = '/upms/organization/listCurrentOrganization',
  // 获取指定组织下所有工厂组织信息
  GET_CHILD_FACTORY_ORG = '/upms/organization/getChildFactoryOrg',
  // 下拉用户列表
  GET_LIST_USER = '/upms/customer/listUser',
  // 校验组织的任务是否存在
  CHECK_EXIST_TASK = '/inventory/task/checkExist',
  // 校验组织引用信息
  CHECK_Tips = '/inventory/task/checkTips'
}

// 全部组织列表
export const getOrganizationList = (): Promise<GetOrganizationListResult> =>
  get(Api.GET_ORGANIZATION_LIST);
// 查询所有子组织列表
export const getChildByCodeList = (): Promise<GetOrganizationListResult> =>
  get(Api.GET_ORGANIZATION_CHILD_LIST);

// 添加组织
export const addOrganization = (params: AddOrganizationParams): Promise<number> =>
  post(Api.ADD_ORGANIZATION, params);

// 编辑组织
export const editOrganization = (params: EditOrganizationParams): Promise<void> =>
  post(Api.EDIT_ORGANIZATION, params);

// 删除组织
export const deleteOrganization = (id: number): Promise<void> =>
  post(Api.DELETE_ORGANIZATION, { id });

// 获取组织树列表
export const getOrganizationTree = (): Promise<GetOrganizationTreeResult[]> =>
  get(Api.GET_ORGANIZATION_TREE, {});

// 新增用户组织关联
export const addUserOrganization = (params: AddUserOrganizationParams): Promise<void> =>
  post(Api.ADD_USER_ORGANIZATION, params);

// 删除用户组织关联
export const deleteUserOrganization = (params: AddUserOrganizationParams): Promise<void> =>
  post(Api.DELETE_USER_ORGANIZATION, params);

// 查看组织详情
export const getOrganizationDetail = (id: number): Promise<GetOrganizationDetailResult> =>
  get(Api.GET_ORGANIZATION_DETAIL, { id });

// 移动组织
export const moveOrganization = (params: MoveOrganizationParams): Promise<void> =>
  post(Api.MOVE_ORGANIZATION, params);

// 获取用户组织树
export const getOrganizationTreeByUser = (userId: number): Promise<GetOrganizationTreeResult[]> =>
  get(Api.GET_ORGANIZATION_TREE_BY_USER, { userId });

// 获取当前用户组织列表
export const getCurrentOrganizationList = (): Promise<GetOrganizationListResult> =>
  get(Api.GET_CURRENT_ORGANIZATION_LIST);

// 获取当前用户组织列表
export const getUserList = (username: string): Promise<GetUserListResult[]> =>
  get(Api.GET_LIST_USER, { username });

/**
 * 校验组织的任务是否存在
 * @param organizationCodeList
 * @param operationType  'MOVE' 移动 | 'EDIT' 编辑 | 'DELETE' 删除
 * @returns
 */
export const postCheckExistTask = (
  organizationCodeList: string[],
  operationType: 'MOVE' | 'EDIT' | 'DELETE'
): Promise<void> => post(Api.CHECK_EXIST_TASK, { organizationCodeList, operationType });

/**
 * 校验组织引用信息
 * @param organizationCode
 * @param operationType  'MOVE' 移动 | 'EDIT' 编辑 | 'DELETE' 删除
 * @returns
 */
export const postCheckTips = (
  organizationCode: string,
  operationType: 'MOVE' | 'EDIT' | 'DELETE',
  targetOrgCode?: string
): Promise<string[]> => post(Api.CHECK_Tips, { organizationCode, operationType, targetOrgCode });

/**
 * 获取指定组织下所有工厂组织信息
 * @param orgCode
 * @returns
 */
export const getChildFactoryOrg = (orgCode: string): Promise<GetChildFactoryOrgResult> =>
  get(Api.GET_CHILD_FACTORY_ORG, { orgCode });
